public without sharing class InstrumentProjectBonusHandler {
    //新建和更新的时候匹配达成率
	/*public static void matchRate(List<InstrumentProjectBonus__c> list_new,Map<Id,InstrumentProjectBonus__c> oldMap){

		Set<Decimal> year = new Set<Decimal>();
		Set<String> quarter = new Set<String>();
		Set<String> person = new Set<String>();
		//Set<Id>  users = new Set<Id >();
		Map<String,InstrumentProjectBonus__c > aRateMap = new Map<String,InstrumentProjectBonus__c>();

    	for(InstrumentProjectBonus__c bouns : list_new){
			
			InstrumentProjectBonus__c old = oldMap != null ? oldMap.get(bouns.Id ) : new InstrumentProjectBonus__c() ;
			if(( bouns.Status__c != '审批中' && bouns.Status__c != '已批准')   ){
				if(bouns.Year__c != null)year.add(bouns.Year__c);
				if(bouns.Quarter__c != null)quarter.add(bouns.Quarter__c);
				if(bouns.EmployeeNumber__c != null)person.add(bouns.EmployeeNumber__c);
				aRateMap.put(bouns.Year__c + bouns.Quarter__c+bouns.EmployeeNumber__c,bouns);
			}


    	}
    
    	for (AchievementRate__c ar  : [select Id,AchievementRate__c ,Year__c,EmployeeNumber__c,Quarter__c from AchievementRate__c 
			where Year__c in: year and Quarter__c=: quarter and EmployeeNumber__c in: person  ] ) {
			String key = ar.Year__c+ar.Quarter__c+ar.EmployeeNumber__c;
			
			if(aRateMap.containsKey(key)){
				InstrumentProjectBonus__c  bouns = aRateMap.get(key);
				if(ar.AchievementRate__c != bouns.AchievementRate__c ) bouns.AchievementRate__c =  ar.AchievementRate__c;


			}
			
		}
	}*/

	//回写到合同BonusFiling__c


	public static void comfirm(List<InstrumentProjectBonus__c> list_new,Map<Id,InstrumentProjectBonus__c> oldMap){

		for(InstrumentProjectBonus__c bouns : list_new){

			InstrumentProjectBonus__c old = oldMap == null ? new InstrumentProjectBonus__c() :  oldMap.get(bouns.Id )  ;
			if(bouns.Status__c == '审批中(子公司经理已确认)'  && bouns.Confirmed__c != true ) bouns.addError('请返回申请单，点击【确认参与人员及贡献率】查看并在确认界面点击【保存】后再审批此申请！注意：1、客户管理属性为【直销部门管理】或者 润达客户  或者 奖金类型为 流水线(搭载i 3000)，还需在确认参与人员及贡献率的画面填写参与人员的贡献率！2、此按钮仅子公司经理、集团区域总监、集团产品总监有权限进行确认！!');
            

		}


		
	}
	
	public static void backWriteBillFill(List<InstrumentProjectBonus__c> list_new,Map<Id,InstrumentProjectBonus__c> oldMap){

		Map<Id,Boolean>  bMap  = new Map<Id,Boolean>();
		List<Task> tlist = new List<Task>();
    	for(InstrumentProjectBonus__c bouns : list_new){
			InstrumentProjectBonus__c old = oldMap == null ? new InstrumentProjectBonus__c() :  oldMap.get(bouns.Id )  ;

			
		    if((bouns.Status__c == '草案中' && old.Status__c != '草案中' )|| (bouns.Status__c == '审批中' && old.Status__c != '审批中' )||(bouns.Status__c == '已拒绝' && old.Status__c != '已拒绝' ) ){
    			Boolean ischange  = (bouns.Status__c  == '审批中' || bouns.Status__c  == '已批准') ?  true : false; 
		    	bMap.put(bouns.Id ,ischange );

			}

    	}
    	if(bMap.keySet().size() == 0)return;


    	// List<Contract__c> conList = new List<Contract__c>();
    	List<Equipment__c > eList = new List<Equipment__c>();
    	Set<String> esets = new Set<String>();
    	for(InstrumentProjectBonusDetails__c idd : [select Id ,InstrumentBonus__c,ApplicationInstrument__c,ApplicationInstrument__r.BonusFiling__c  from InstrumentProjectBonusDetails__c where InstrumentBonus__c in: bMap.keySet() ]){


    		if(idd.ApplicationInstrument__r.BonusFiling__c != bMap.get(idd.InstrumentBonus__c ) ){
    			Equipment__c eq = new Equipment__c();
    			eq.Id = idd.ApplicationInstrument__c;
    			eq.BonusFiling__c = bMap.get(idd.InstrumentBonus__c ) ;
    			if(!esets.contains(eq.Id ))eList.add(eq);
    			esets.add(eq.Id);
    		}

    	}
    	// for(Equipment__c eq : [select BonusFiling__c,RelatedBonusDetails__r.InstrumentBonus__c , Id from Equipment__c where  RelatedBonusDetails__r.InstrumentBonus__c  in:bMap.keySet()  ]){

    	// 	if(eq.BonusFiling__c != bMap.get(eq.RelatedBonusDetails__r.InstrumentBonus__c ) ){
    	// 		eq.BonusFiling__c = bMap.get(eq.RelatedBonusDetails__r.InstrumentBonus__c ) ;
    	// 		eList.add(eq);
    	// 	}
    	// }
    	
    	





   //  	for(InstrumentProjectBonusDetails__c bd :  [select Id ,ApplicationInstrument__c ,ApplicationInstrument__r.BonusFiling__c ,Contract__c,Contract__r.BonusFiling__c ,InstrumentBonus__r.Status__c
   //  	  from InstrumentProjectBonusDetails__c where InstrumentBonus__c in : bMap.keySet() ]){
   //  		Boolean ischange  = (bd.InstrumentBonus__r.Status__c  == '审批中' || bd.InstrumentBonus__r.Status__c  == '已批准') ?  true : false; 
			// if(bd.Contract__c != null  && ischange != bd.Contract__r.BonusFiling__c )conList.add(new Contract__c (Id=bd.Contract__c,BonusFiling__c =ischange ));
			// if(bd.ApplicationInstrument__c	!= null && ischange != bd.ApplicationInstrument__r.BonusFiling__c )eList.add(new Equipment__c (Id=bd.ApplicationInstrument__c,BonusFiling__c = ischange));

   //  	}
    	// if(conList.size() > 0 ) update conList;
    	if(eList.size() > 0 ) update eList;
	}
	
    
}